package com.cellulam.properties;

import com.cellulam.framework.core.config.YamlPropertySourceFactory;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 系统相关属性
 */
@Component
@Getter
@PropertySource(value = {"classpath:${spring.profiles.active}/default-config.yml"}, ignoreResourceNotFound = true, factory = YamlPropertySourceFactory.class)
public class SystemProperties {

    @Autowired
    private Environment environment;

    @Value("${spring.application.name:}")
    private String applicationName;

    @Value("${spring.profiles.active:}")
    private String activeProfile;

    @Value("${spring.datasource.type:}")
    private Class<? extends DataSource> dataSourceType;

    @Value("${spring.datasource.hikari.initial-size:5}")
    private Integer dbPoolInitialSize;

    @Value("${spring.datasource.hikari.minimum-idle:5}")
    private Integer dbPoolMinimumIdle;

    @Value("${spring.datasource.hikari.maximum-pool-size:20}")
    private Integer dbPoolMaximumPoolSize;

    @Value("${spring.datasource.hikari.auto-commit:true}")
    private Boolean dbPoolAutoCommit;

    @Value("${spring.datasource.hikari.idle-timeout:600000}")
    private Integer dbPoolIdleTimeout;

    @Value("${spring.datasource.hikari.max-lifetime:1800000}")
    private Integer dbPoolMaxLifetime;

    @Value("${spring.datasource.hikari.connection-timeout:1000}")
    private Integer dbPoolConnectionTimeout;

    @Value("${spring.datasource.hikari.connection-test-query:select 1}")
    private String dbPoolConnectionTestQuery;


    @Value("${mybatis.mapper-locations:}")
    private String mybatisMapperLocations;

    @Value("${mybatis.config-location:}")
    private String mybatisConfigLocation;

    @Value("${mybatis.type-aliases-package:}")
    private String mybatisTypeAliasesPackage;

    @Value("${spring.datasource.names:}")
    private String dataSourceNames;

    @Value("${sql.timeout:1000}")
    private Integer sqlTimeout;

    @Value("${http.timeout:1000}")
    private Integer httpTimeout;

    @Value("${log.durable:false}")
    private Boolean durableLog;

    @Value("${service.api.host.cellulam-basic:}")
    private String cellulamBasicHost;

    @Value("${access.ignore.url:}")
    private String accessIgnore;

    @Value("${ali.mq.name-srv:}")
    private String aliMqNameSrv;

    @Value("${ali.mq.access-key:}")
    private String aliMqAccessKey;

    @Value("${ali.mq.secret-key:}")
    private String aliMqSecretKey;

    /**
     * 是否是schedule任务，默认为web应用
     */
    @Value("${schedule:false}")
    private boolean schedule;

    public String getProperty(String name) {
        return environment.getProperty(name);
    }

    public <T> T getProperty(String name, Class<T> aClass) {
        return environment.getProperty(name, aClass);
    }
}

